#ifndef CSMARTPUTTY_H
#define CSMARTPUTTY_H

#include <QMainWindow>
#include <QtAlgorithms>
#include <QString>
#include<QVector>
#include <QFile>

////////////////////////////
#define S_COMMON_ERROR -1
#define S_SUCCESS 0

#define S_CONN_TABLE_COL_CNT 4
#define S_CONN_TABLE_NAME_IDX 0
#define S_CONN_TABLE_IP_IDX 1
#define S_CONN_TABLE_USER_IDX 2
#define S_CONN_TABLE_PASSWD_IDX 3

#define S_CONN_CFG "C:\\Users\\hb\\Desktop\\smartPutty\\build-smartPutty-Desktop_Qt_5_7_0_MinGW_32bit-Debug\\debug\\cfg.ini"

namespace Ui {
class CSmartPutty;
}

class CLoginCfgMgr
{
public:
    typedef struct {
        int m_idx;
        QString m_name;
        QString m_ip;
        QString m_user;
        QString m_passwd;

        bool match(QString strKey)
        {
            return m_name.indexOf(strKey)>=0
                    || m_ip.indexOf(strKey)>=0
                    || m_user.indexOf(strKey)>=0;
        }
    } TConnectItem;
public:
    CLoginCfgMgr(){}
    ~CLoginCfgMgr(){}

    static CLoginCfgMgr* singleton();

    int refresh(){return init();}

    QVector<CLoginCfgMgr::TConnectItem>& getConn(){return m_connItemVec;}
    QVector<CLoginCfgMgr::TConnectItem>& getConn(QString strKey);
    CLoginCfgMgr::TConnectItem& getConn(int idx);

    int addConnectItem(CLoginCfgMgr::TConnectItem& item);
private:
    void setCfg(QString strCfg);

    int init();
    int read_plain_txt_cfg(QString strCfg);
    int read_plain_txt_item(QFile& file);
    int read_plain_txt_line(QFile& file,QString& strVal);
private:
    static CLoginCfgMgr* m_pSingleton;

    QString m_strCfgName;

    QVector<TConnectItem> m_connItemVec;
    QVector<TConnectItem> m_matchConnItemVec;
};

class CSSHLoginHelper
{
public:
    CSSHLoginHelper(){}
    ~CSSHLoginHelper(){}

    static int login(CLoginCfgMgr::TConnectItem& item);
};

class CSmartPutty : public QMainWindow
{
    Q_OBJECT

public:
    explicit CSmartPutty(QWidget *parent = 0);
    ~CSmartPutty();

    int init();
    int displayConn(QVector<CLoginCfgMgr::TConnectItem>& connVec);

private:
    int getConnIdx(QString strConn);
    void printLog(QString log);

private slots:
    void on_filterEdit_textChanged(const QString &arg1);

    void on_connsView_doubleClicked(const QModelIndex &index);

    void on_connsView_clicked(const QModelIndex &index);

    void on_add_connect_clicked();

    //右击菜单
    void show_context_menu(const QPoint& pos);
    //void show_context_menu_signal(const QPoint& pos);
    void show_get_file_menu();
    void show_put_file_menu();

private:
    Ui::CSmartPutty *ui;
};

#endif // CSMARTPUTTY_H
